<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        // 迭代器的使用可以极大地简化数据操作，于是ES6向JS中添加了这个迭代器特性

        function makeIterator(arr){

            //描述当前需要获取的数组的元素数据的索引(下标)
            let nextIndex=0;
            return {
                next:function(){
                    if(nextIndex<arr.length){
                        //获取当前下标对应的元素值
                        let current=arr[nextIndex];
                        nextIndex++;//1

                        //value属性表示当前成员，done表示的遍历是否结束
                        return {value:current,done:false}
                    }else{
                        return {value:undefined,done:true}
                    }
                }
            }
        }
        // let arrs=[11,22,33,44];
        // let arrsIt=makeIterator(arrs);//对象
        // console.log(arrsIt);
        // let data1=arrsIt.next();
        // console.log(data1);
        // let data2=arrsIt.next();
        // console.log(data2);
        // let data3=arrsIt.next();
        // console.log(data3);
        // let data4=arrsIt.next();
        // console.log(data4);
        // let data5=arrsIt.next();
        // console.log(data5);
        let arrs=[11,22,33,44];
        let arrsIt=makeIterator(arrs);
        do{
            var {value,done}=arrsIt.next();
            if(done){
               break; 
            }
            console.log(value);
        }while(!done);
        var arrays=new Array("alice","baby","cici","lili");
        // console.log(arrays);
        var arraysIt2=arrays[Symbol.iterator]();
        console.log(arraysIt2.next().value);
        console.log(arraysIt2.next().value);
        console.log(arraysIt2.next().value);
        console.log(arraysIt2.next().value);
        console.log(arraysIt2.next().value);

        // ES6中for...of遍历可迭代对象，不能遍历普通对象(没有Symbol.iterator方法)
        for(var i of arrays){
            console.log(i);
        }
    </script>
</head>
<body>
    
</body>
</html>